package com.by.controller;


import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Product;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.query.ProductCustomerQuery;
import com.by.query.ProductQuery;
import com.by.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "产品管理")
@RequestMapping("/v1")
public class ProductController extends BaseController {
    @Autowired
    ProductService productService;

    @ApiOperation("查询所有产品")
    @GetMapping("/product/all")
    public ResultBean queryAll() {
        List list = productService.queryAll();
        return success("产品", list);
    }
    @ApiOperation("产品条件查询")
    @PostMapping("/product/query")
    public ResultBean query(@RequestBody ProductQuery productQuery) {
        PageBean bean = productService.query(BeanUtil.beanToMap(productQuery));
        return success("产品列表", bean);
    }

    @ApiOperation("删除产品")
    @DeleteMapping("/product")
    public ResultBean delete(@RequestParam(value = "id", required = true) int id) {
        int res = productService.delete(id);
        return deleteResult(res);
    }

    @ApiOperation("新增产品")
    @PostMapping("/product")
    public ResultBean save(@RequestBody Product product) {
        int res = productService.save(product);
        return saveResult(res);
    }

    @ApiOperation("修改产品信息")
    @PutMapping("/product")
    public ResultBean update(@RequestBody Product product) {
        int res = productService.update(product);
        return updateResult(res);
    }

    @ApiOperation("客户端产品分页查询")
    @PostMapping("/product/cust")
    public ResultBean queryCustomer(@RequestBody ProductCustomerQuery productCustomerQuery) {
        PageBean bean = productService.queryCust(BeanUtil.beanToMap(productCustomerQuery));
        return success("客户端产品列表", bean);
    }

    @ApiOperation("产品详情")
    @GetMapping("/product")
    public ResultBean getById(@RequestParam(value = "id", required = true) int id) {
        Product product = productService.get(id);
        return success("产品详情", product);
    }
}
